################################################################################
###########################   Variable centering   #############################
################################################################################

library(labelled)
library(dplyr)
library(psych)
options(max.print = 99999)
rm(list=ls())

load("lapop_pela_select.RData")
base=lapop_pela_select; rm(lapop_pela_select)


#Remove Venezuela
base2 = filter(base, base$paisR != "Venezuela")

#Remove Honduras
#To reproduce influential cases analysis do not remove Honduras
base2 = filter(base2, base2$paisR != "Honduras")

#Select variables
base2=dplyr::select(
  base2, id_unico, wave, paisF, paisR, pais.ano, id_lapop, id_pela, year,
  conf_parlamnt, eval_econpais, eval_econpais_Dummy, conf_interpers, voto_presid,
  educacion, eduniv, urbano, mujer, edad, casado, izq, ideolegis_sd_w,
  sexo_media_w, familiar_pol_media_w, recambio_media_w, workers.prop_w, e_gdppc, 
  v2x_corr, desempleo) %>% na.omit()

################################    Centering    ###############################

### LAPOP variables (individual-level) ###
#Left ideology
base2$izq_gm = base2$izq - mean(base2$izq, na.rm=T)
izq_pelas=base2 %>% group_by(id_pela) %>% summarise(izq_pela=mean(izq, na.rm=T))
izq_encs=base2 %>% group_by(pais.ano) %>% summarise(izq_pais.ano=mean(izq, na.rm=T))
izq_paiss=base2 %>% group_by(paisR) %>% summarise(izq_pais=mean(izq, na.rm=T))
base2=merge(base2, izq_pelas, all=T); base2=merge(base2, izq_encs, all=T); base2=merge(base2, izq_paiss, all=T)
base2$izq_gc.pela=base2$izq-base2$izq_pela
base2$izq_gc.pano=base2$izq-base2$izq_pais.ano
base2$izq_gc.pais=base2$izq-base2$izq_pais

#Presidential turnout
base2$voto_presid_gm = base2$voto_presid - mean(base2$voto_presid, na.rm=T)
voto_presid_pelas=base2 %>% group_by(id_pela) %>% summarise(voto_presid_pela=mean(voto_presid, na.rm=T))
voto_presid_encs=base2 %>% group_by(pais.ano) %>% summarise(voto_presid_pais.ano=mean(voto_presid, na.rm=T))
voto_presid_paiss=base2 %>% group_by(paisR) %>% summarise(voto_presid_pais=mean(voto_presid, na.rm=T))
base2=merge(base2, voto_presid_pelas, all=T); base2=merge(base2, voto_presid_encs, all=T); base2=merge(base2, voto_presid_paiss, all=T)
base2$voto_presid_gc.pela=base2$voto_presid-base2$voto_presid_pela
base2$voto_presid_gc.pano=base2$voto_presid-base2$voto_presid_pais.ano
base2$voto_presid_gc.pais=base2$voto_presid-base2$voto_presid_pais

#Social trust
base2$conf_interpers_gm = base2$conf_interpers - mean(base2$conf_interpers, na.rm=T)
conf_interpers_pelas=base2 %>% group_by(id_pela) %>% summarise(conf_interpers_pela=mean(conf_interpers, na.rm=T))
conf_interpers_encs=base2 %>% group_by(pais.ano) %>% summarise(conf_interpers_pais.ano=mean(conf_interpers, na.rm=T))
conf_interpers_paiss=base2 %>% group_by(paisR) %>% summarise(conf_interpers_pais=mean(conf_interpers, na.rm=T))
base2=merge(base2, conf_interpers_pelas, all=T); base2=merge(base2, conf_interpers_encs, all=T); base2=merge(base2, conf_interpers_paiss, all=T)
base2$conf_interpers_gc.pela=base2$conf_interpers-base2$conf_interpers_pela
base2$conf_interpers_gc.pano=base2$conf_interpers-base2$conf_interpers_pais.ano
base2$conf_interpers_gc.pais=base2$conf_interpers-base2$conf_interpers_pais

#economic assessment of the country
base2$eval_econpais_gm = base2$eval_econpais - mean(base2$eval_econpais, na.rm=T)
eval_econpais_pelas=base2 %>% group_by(id_pela) %>% summarise(eval_econpais_pela=mean(eval_econpais, na.rm=T))
eval_econpais_encs=base2 %>% group_by(pais.ano) %>% summarise(eval_econpais_pais.ano=mean(eval_econpais, na.rm=T))
eval_econpais_paiss=base2 %>% group_by(paisR) %>% summarise(eval_econpais_pais=mean(eval_econpais, na.rm=T))
base2=merge(base2, eval_econpais_pelas, all=T); base2=merge(base2, eval_econpais_encs, all=T); base2=merge(base2, eval_econpais_paiss, all=T)
base2$eval_econpais_gc.pela=base2$eval_econpais-base2$eval_econpais_pela
base2$eval_econpais_gc.pano=base2$eval_econpais-base2$eval_econpais_pais.ano
base2$eval_econpais_gc.pais=base2$eval_econpais-base2$eval_econpais_pais

#economic assessment of the country (as dummy)
base2$eval_econpais_Dummy_gm = base2$eval_econpais_Dummy - mean(base2$eval_econpais_Dummy, na.rm=T)
eval_econpais_Dummy_pelas=base2 %>% group_by(id_pela) %>% summarise(eval_econpais_Dummy_pela=mean(eval_econpais_Dummy, na.rm=T))
eval_econpais_Dummy_encs=base2 %>% group_by(pais.ano) %>% summarise(eval_econpais_Dummy_pais.ano=mean(eval_econpais_Dummy, na.rm=T))
eval_econpais_Dummy_paiss=base2 %>% group_by(paisR) %>% summarise(eval_econpais_Dummy_pais=mean(eval_econpais_Dummy, na.rm=T))
base2=merge(base2, eval_econpais_Dummy_pelas, all=T); base2=merge(base2, eval_econpais_Dummy_encs, all=T); base2=merge(base2, eval_econpais_Dummy_paiss, all=T)
base2$eval_econpais_Dummy_gc.pela=base2$eval_econpais_Dummy-base2$eval_econpais_Dummy_pela
base2$eval_econpais_Dummy_gc.pano=base2$eval_econpais_Dummy-base2$eval_econpais_Dummy_pais.ano
base2$eval_econpais_Dummy_gc.pais=base2$eval_econpais_Dummy-base2$eval_econpais_Dummy_pais

#Urban
base2$urbano_gm = base2$urbano - mean(base2$urbano, na.rm=T)
urbano_pelas=base2 %>% group_by(id_pela) %>% summarise(urbano_pela=mean(urbano, na.rm=T))
urbano_encs=base2 %>% group_by(pais.ano) %>% summarise(urbano_pais.ano=mean(urbano, na.rm=T))
urbano_paiss=base2 %>% group_by(paisR) %>% summarise(urbano_pais=mean(urbano, na.rm=T))
base2=merge(base2, urbano_pelas, all=T); base2=merge(base2, urbano_encs, all=T); base2=merge(base2, urbano_paiss, all=T)
base2$urbano_gc.pela=base2$urbano-base2$urbano_pela
base2$urbano_gc.pano=base2$urbano-base2$urbano_pais.ano
base2$urbano_gc.pais=base2$urbano-base2$urbano_pais

#Female
base2$mujer_gm = base2$mujer - mean(base2$mujer, na.rm=T)
mujer_pelas=base2 %>% group_by(id_pela) %>% summarise(mujer_pela=mean(mujer, na.rm=T))
mujer_encs=base2 %>% group_by(pais.ano) %>% summarise(mujer_pais.ano=mean(mujer, na.rm=T))
mujer_paiss=base2 %>% group_by(paisR) %>% summarise(mujer_pais=mean(mujer, na.rm=T))
base2=merge(base2, mujer_pelas, all=T); base2=merge(base2, mujer_encs, all=T); base2=merge(base2, mujer_paiss, all=T)
base2$mujer_gc.pela=base2$mujer-base2$mujer_pela
base2$mujer_gc.pano=base2$mujer-base2$mujer_pais.ano
base2$mujer_gc.pais=base2$mujer-base2$mujer_pais

#Age
base2$edad_gm = base2$edad - mean(base2$edad, na.rm=T)
edad_pelas=base2 %>% group_by(id_pela) %>% summarise(edad_pela=mean(edad, na.rm=T))
edad_encs=base2 %>% group_by(pais.ano) %>% summarise(edad_pais.ano=mean(edad, na.rm=T))
edad_paiss=base2 %>% group_by(paisR) %>% summarise(edad_pais=mean(edad, na.rm=T))
base2=merge(base2, edad_pelas, all=T); base2=merge(base2, edad_encs, all=T); base2=merge(base2, edad_paiss, all=T)
base2$edad_gc.pela=base2$edad-base2$edad_pela
base2$edad_gc.pano=base2$edad-base2$edad_pais.ano
base2$edad_gc.pais=base2$edad-base2$edad_pais

#Centrado education
base2$educacion_gm = base2$educacion - mean(base2$educacion, na.rm=T)
educacion_pelas=base2 %>% group_by(id_pela) %>% summarise(educacion_pela=mean(educacion, na.rm=T))
educacion_encs=base2 %>% group_by(pais.ano) %>% summarise(educacion_pais.ano=mean(educacion, na.rm=T))
educacion_paiss=base2 %>% group_by(paisR) %>% summarise(educacion_pais=mean(educacion, na.rm=T))
base2=merge(base2, educacion_pelas, all=T); base2=merge(base2, educacion_encs, all=T); base2=merge(base2, educacion_paiss, all=T)
base2$educacion_gc.pela=base2$educacion-base2$educacion_pela
base2$educacion_gc.pano=base2$educacion-base2$educacion_pais.ano
base2$educacion_gc.pais=base2$educacion-base2$educacion_pais

#Tertiary education (dummy)
base2$eduniv_gm = base2$eduniv - mean(base2$eduniv, na.rm=T)
eduniv_pelas=base2 %>% group_by(id_pela) %>% summarise(eduniv_pela=mean(eduniv, na.rm=T))
eduniv_encs=base2 %>% group_by(pais.ano) %>% summarise(eduniv_pais.ano=mean(eduniv, na.rm=T))
eduniv_paiss=base2 %>% group_by(paisR) %>% summarise(eduniv_pais=mean(eduniv, na.rm=T))
base2=merge(base2, eduniv_pelas, all=T); base2=merge(base2, eduniv_encs, all=T); base2=merge(base2, eduniv_paiss, all=T)
base2$eduniv_gc.pela=base2$eduniv-base2$eduniv_pela
base2$eduniv_gc.pano=base2$eduniv-base2$eduniv_pais.ano
base2$eduniv_gc.pais=base2$eduniv-base2$eduniv_pais

#Married
base2$casado_gm = base2$casado - mean(base2$casado, na.rm=T)
casado_pelas=base2 %>% group_by(id_pela) %>% summarise(casado_pela=mean(casado, na.rm=T))
casado_encs=base2 %>% group_by(pais.ano) %>% summarise(casado_pais.ano=mean(casado, na.rm=T))
casado_paiss=base2 %>% group_by(paisR) %>% summarise(casado_pais=mean(casado, na.rm=T))
base2=merge(base2, casado_pelas, all=T); base2=merge(base2, casado_encs, all=T); base2=merge(base2, casado_paiss, all=T)
base2$casado_gc.pela=base2$casado-base2$casado_pela
base2$casado_gc.pano=base2$casado-base2$casado_pais.ano
base2$casado_gc.pais=base2$casado-base2$casado_pais


### PELA variables (legislature-level) ###

#Legislative Turnover to Reelection
base2$reeleccion <- 1 - base2$recambio_media_w
summary(base2$reeleccion)
base2$reeleccion_gm = base2$reeleccion - mean(base2$reeleccion, na.rm=T)
base2$reeleccion_prom<- ave(base2$reeleccion, base2$paisF, FUN = mean)
base2$reeleccion_gc <- base2$reeleccion - base2$reeleccion_prom

##Relatives in Politics
base2$fampol_gm = base2$familiar_pol_media_w - mean(base2$familiar_pol_media_w, na.rm=T)
base2$fampol_prom<- ave(base2$familiar_pol_media_w, base2$paisF, FUN = mean)
base2$fampol_gc <- base2$familiar_pol_media_w - base2$fampol_prom

#Ideological Homogeneity
base2$homo_ideo=((base2$ideolegis_sd_w- max(base2$ideolegis_sd_w)) * -1) 
summary(base2$homo_ideo)
base2$homo_ideo_gm = base2$homo_ideo - mean(base2$homo_ideo, na.rm=T)
base2$homo_ideo_prom<- ave(base2$homo_ideo, base2$paisF, FUN = mean)
base2$homo_ideo_gc <- base2$homo_ideo - base2$homo_ideo_prom

#Proportion of Male
base2$sexo_inv =  1- base2$sexo_media_w
base2$sexo_inv_gm = base2$sexo_inv - mean(base2$sexo_inv, na.rm=T)
base2$sexo_inv_prom<- ave(base2$sexo_inv, base2$paisF, FUN = mean)
base2$sexo_inv_gc <- base2$sexo_inv - base2$sexo_inv_prom

#Proportion of working class
base2$workers_inv =  1- base2$workers.prop_w
base2$workers_inv_gm = base2$workers_inv - mean(base2$workers_inv, na.rm=T)
base2$workers_inv_prom<- ave(base2$workers_inv, base2$paisF, FUN = mean)
base2$workers_inv_gc <- base2$workers_inv - base2$workers_inv_prom

## V-DEM variables ##

#GDP Percapita      
base2$e_gdppc_gm = base2$e_gdppc - mean(base2$e_gdppc, na.rm=T)
e_gdppc_means=base2 %>% group_by(paisR) %>% summarise(e_gdppc_means=mean(e_gdppc, na.rm=T))
base2=merge(base2, e_gdppc_means, all=T)
base2$e_gdppc_gc=base2$e_gdppc-base2$e_gdppc_means

#Political corruption
base2$v2x_corr_gm = base2$v2x_corr - mean(base2$v2x_corr, na.rm=T)
v2x_corr_means=base2 %>% group_by(paisR) %>% summarise(v2x_corr_means=mean(v2x_corr, na.rm=T))
base2=merge(base2, v2x_corr_means, all=T)
base2$v2x_corr_gc=base2$v2x_corr-base2$v2x_corr_means

## World Bank variables

base2$desempleo_gm = base2$desempleo - mean(base2$desempleo, na.rm=T)
desempleo_means=base2 %>% group_by(paisR) %>% summarise(desempleo_means=mean(desempleo, na.rm=T))
base2=merge(base2, desempleo_means, all=T)
base2$desempleo_gc=base2$desempleo-base2$desempleo_means

save(base2, file="lapop_pela_select_cent_wVH.RData")

#If do not remove Honduras:
#save(base2, file="lapop_pela_select_cent.RData")
